package game;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * @author Lucas 'Mezmor' Pfister
 *
 */
public class AssetManager {
	BufferedImage roads, plains, river, ocean, lake;
		
	
	
	public AssetManager(){
		 try {
			roads = ImageIO.read(new File("assets/terrain/roads4x3.png"));
			plains = ImageIO.read(new File("assets/terrain/plains4x3.png"));
			river = ImageIO.read(new File("assets/terrain/river3x3.png"));
			ocean = ImageIO.read(new File("assets/terrain/ocean3x3.png"));
			lake = ImageIO.read(new File("assets/terrain/lake3x3.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * The input code is the encoding used in a TileMap.
	 * The encoding is composed of 4 single-digit numbers.
	 * 		First	: Index of the picture. 0 = roads, 1 = plains, 2 = river, 3 = ocean, 4 = lake
	 * 		Second	: X-coordinate in the sprite sheet
	 * 		Third	: Y-coordinate in the sprite sheet
	 * 
	 * @param code
	 * @return
	 */
	public Image getTerrain(String code){
		char[] charCode = code.toCharArray();
		int typeIndex = Character.getNumericValue(charCode[0]);
		int x = Character.getNumericValue(charCode[1]);
		int y = Character.getNumericValue(charCode[2]);
		
		switch(typeIndex){
			case 0: // Roads
				return roads.getSubimage(x*32, y*32, 32, 32);
			case 1: // Plains
				return plains.getSubimage(x*32, y*32, 32, 32);
			case 2: // River
				return river.getSubimage(x*32, y*32, 32, 32);
			case 3: // Ocean
				return ocean.getSubimage(x*32, y*32, 32, 32);
			case 4: // Lake
				return lake.getSubimage(x*32, y*32, 32, 32);
			default: // Not found
				throw new IllegalArgumentException("Referenced spritesheet does not exist!");
		}
	}
	
	
}